/*
 *  Copyright 2009 Advanced Industrial Science and Technology (AIST).
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/*! \file
 *  \brief
 */
#pragma one

#include <thrust/graph/adjacency_list.hpp>
#include <thrust/copy.h>
namespace thrust {

  template <class Config>
  __host__
  std::ostream&
  operator<<( std::ostream& out, 
              directed_graph_helper<Config>& g_)
  {
    typedef typename Config::graph_type Graph;
    typedef typename Config::vertex_iterator vertex_iterator;
    typedef typename Config::out_edge_iterator out_edge_iterator;
    typedef typename Config::adjacency_iterator adjacency_iterator;
    typedef typename Config::edge_iterator edge_iterator;
    typedef typename Config::edge_descriptor edge_descriptor;
    typedef typename Config::vertex_descriptor vertex_descriptor;

    typedef typename Config::StoredVertex StoredVertex;

    Graph& g =static_cast<Graph&>(g_);

    out <<num_vertices(g) <<" vertices("
        <<capacity_vertices(g) <<"), "
        <<num_edges(g) <<" edges(" 
        <<capacity_edges(g) <<")"
        <<std::endl;
    
    vertex_iterator viter, vlast;
    tie( viter, vlast) =vertices( g);

    for( ; viter !=vlast; ++viter){

      out <<*viter <<", out:(" 
          <<out_degree( *viter, g) <<")[";
      out_edge_iterator out_iter, out_last;
      tie( out_iter, out_last) =out_edges( *viter, g);
      for( ; out_iter !=out_last; ++out_iter){
        out <<target( *out_iter, g) <<",";
      }
      out <<"]";

      out <<", adj:[";
      adjacency_iterator adj_iter, adj_last;
      tie( adj_iter, adj_last) =adjacent_vertices( *viter, g);
      for( ; adj_iter !=adj_last; ++adj_iter)
        out <<*adj_iter <<",";
      out <<"]";
      
      out <<std::endl;
    }

    out <<num_edges( g) <<" edges" <<std::endl;
    edge_iterator edge_iter, edge_last;
    thrust::tie( edge_iter, edge_last) =edges( g);
    for( ; edge_iter !=edge_last; ++edge_iter){
      out <<*edge_iter <<",";
    }
    out<<std::endl;

#ifdef DEBUG
    out <<"out_edges container:(" <<capacity_edges( g) <<")" <<std::endl;
    thrust::tie( edge_iter, edge_last) =detail::all_out_edges( g);
    for( ; edge_iter !=edge_last; ++edge_iter){
      out <<*edge_iter <<std::endl;
    }
    out<<std::endl;
#endif

    return out;
  }

  template <class Config>
  __host__
  std::ostream&
  operator<<( std::ostream& out, 
              undirected_graph_helper<Config>& g_)
  {
    typedef typename Config::graph_type        graph_type;
    typedef typename Config::vertex_iterator   vertex_iterator;
    typedef typename Config::out_edge_iterator out_edge_iterator;
    typedef typename Config::in_edge_iterator  in_edge_iterator;
    typedef typename Config::adjacency_iterator adjacency_iterator;
    typedef typename Config::edge_iterator edge_iterator;
    
    graph_type& g =static_cast<graph_type&>(g_);

    out <<num_vertices(g) <<" vertices("
        <<capacity_vertices(g) <<"), "
        <<num_edges(g) <<" edges("
        <<capacity_edges(g) <<")"
        <<std::endl;
    
    vertex_iterator viter, vlast;
    tie( viter, vlast) =vertices( g);
    for( ; viter !=vlast; ++viter){
      

      out <<*viter <<", out:("
	  <<thrust::out_degree( *viter, g) <<")[";
      out_edge_iterator out_iter, out_last;
      tie( out_iter, out_last) =thrust::out_edges( *viter, g);
      for( ; out_iter !=out_last; ++out_iter){
        out <<target( *out_iter, g) <<",";
      }
      out <<"]";

      out <<", adj:[";
      adjacency_iterator adj_iter, adj_last;
      tie( adj_iter, adj_last) =adjacent_vertices( *viter, g);
      for( ; adj_iter !=adj_last; ++adj_iter)
        out <<*adj_iter <<",";
      out <<"]";

      out <<std::endl;
    }
    
    out <<num_edges( g) <<" edges" <<std::endl;
    edge_iterator edge_iter, edge_last;
    tie( edge_iter, edge_last) =edges( g);

    for( ; edge_iter !=edge_last; ++edge_iter){
      out <<*edge_iter <<",";
    }
    out<<std::endl;

#ifdef DEBUG
    out <<"out_edges container:(" <<capacity_edges( g) <<")" <<std::endl;
    thrust::tie( edge_iter, edge_last) =detail::all_out_edges( g);
    for( ; edge_iter !=edge_last; ++edge_iter){
      out <<*edge_iter <<std::endl;
    }
    out<<std::endl;

    out <<detail::num_in_edges( g) <<" in_edges" <<std::endl;
    thrust::tie( edge_iter, edge_last) =detail::in_edges( g);
    for( ; edge_iter !=edge_last; ++edge_iter){
      out <<*edge_iter <<",";
    }
    out <<std::endl;
    
    out <<"in_edges container:(" 
        <<detail::capacity_in_edges( g) 
        <<")" <<std::endl;
    thrust::tie( edge_iter, edge_last) =detail::all_in_edges( g);
    for( ; edge_iter !=edge_last; ++edge_iter){
      out <<*edge_iter <<std::endl;
    }
    out<<std::endl;
#endif
    
    return out;
  }

  template <class Config>
  __host__
  std::ostream&
  operator<<( std::ostream& out, 
              bidirectional_graph_helper<Config>& g_)
  {
    typedef typename Config::graph_type        graph_type;
    typedef typename Config::vertex_iterator   vertex_iterator;
    typedef typename Config::out_edge_iterator out_edge_iterator;
    typedef typename Config::in_edge_iterator  in_edge_iterator;
    typedef typename Config::adjacency_iterator adjacency_iterator;
    typedef typename Config::edge_iterator      edge_iterator;
    
    graph_type& g =static_cast<graph_type&>(g_);
    
    out <<num_vertices(g) <<" vertices("
        <<capacity_vertices(g) <<"), "
        <<num_edges(g) <<" edges( "
        <<capacity_edges(g) <<")"
        <<std::endl;
    
    vertex_iterator viter, vlast;
    tie( viter, vlast) =vertices( g);
    for( ; viter !=vlast; ++viter){
      
      out <<*viter <<", out:("
          <<thrust::out_degree( *viter, g) <<")[";
      out_edge_iterator out_iter, out_last;
      tie( out_iter, out_last) =thrust::out_edges( *viter, g);
      for( ; out_iter !=out_last; ++out_iter){
        out <<target( *out_iter, g) <<",";
      }
      out <<"]";
      
      out <<", in:(" 
          <<thrust::in_degree( *viter, g) <<")[";
      in_edge_iterator in_iter, in_last;
      tie( in_iter, in_last) =in_edges( *viter, g);
      for( ; in_iter !=in_last; ++in_iter){
        out <<source( *in_iter, g) <<",";
      }
      out <<"]";

      out <<", adj:[";
      adjacency_iterator adj_iter, adj_last;
      tie( adj_iter, adj_last) =adjacent_vertices( *viter, g);
      for( ; adj_iter !=adj_last; ++adj_iter)
        out <<*adj_iter <<",";
      out <<"]";
      
      out<<std::endl;
    }

    out <<num_edges( g) <<" edges" <<std::endl;
    edge_iterator edge_iter, edge_last;
    thrust::tie( edge_iter, edge_last) =edges( g);
    for( ; edge_iter !=edge_last; ++edge_iter){
      out <<*edge_iter <<",";
    }
    out <<std::endl;

#ifdef DEBUG
    out <<"out_edges container:(" <<capacity_edges( g) <<")" <<std::endl;
    thrust::tie( edge_iter, edge_last) =detail::all_out_edges( g);
    for( ; edge_iter !=edge_last; ++edge_iter){
      out <<*edge_iter <<std::endl;
    }
    out<<std::endl;

    out <<detail::num_in_edges( g) <<" in_edges(" 
        <<detail::capacity_in_edges(g) <<")"
        <<std::endl;
    thrust::tie( edge_iter, edge_last) =thrust::detail::in_edges( g);
    for( ; edge_iter !=edge_last; ++edge_iter){
      out <<*edge_iter <<",";
    }
    out <<std::endl;
#endif

    return out;
  }
}
